package xxl.robotlegs.extensions.modularity 
{
	import flash.display.DisplayObjectContainer;
	import xxl.robotlegs.extensions.contextview.impl.ContextView;
	import xxl.robotlegs.extensions.matching.impl.InstanceOfMatcher;
	import xxl.robotlegs.extensions.modularity.api.IModuleConnector;
	import xxl.robotlegs.extensions.modularity.impl.ContextViewBasedExistenceWatcher;
	import xxl.robotlegs.extensions.modularity.impl.ModularContextEvent;
	import xxl.robotlegs.extensions.modularity.impl.ModuleConnector;
	import xxl.robotlegs.extensions.modularity.impl.ViewManagerBasedExistenceWatcher;
	import xxl.robotlegs.extensions.viewmanager.api.IViewManager;
	import xxl.robotlegs.framework.api.IContext;
	import xxl.robotlegs.framework.api.IExtension;
	import xxl.robotlegs.framework.api.IInjector;
	import xxl.robotlegs.framework.api.ILogger;
	import xxl.signals.impl.natives.sets.DisplayObjectSignalSet;
	
	/**
	 * ...
	 * @author XXL
	 */
	public class ModularityExtension implements IExtension 
	{
		private var _context:IContext;
		private var _injector:IInjector;
		private var _logger:ILogger;
		private var _inherit:Boolean;
		private var _expose:Boolean;
		private var _contextView:ContextView;
		
		public function ModularityExtension(inherit:Boolean = true, expose:Boolean = true) 
		{
			_inherit = inherit;
			_expose = expose;
		}
		
		public function toString():String
		{
			return "ModularityExtension：（简介：提供一个IModuleConnector让不同模块之间可以相互通讯。必要依赖：ContextSignalExtension）";
		}
		
		private function beforeInitializing():void 
		{
			_contextView || _logger.error("没有ContextView，ModularityExtension需要指定一个ContextView");
		}
		
		private function handleContextView(contextView:ContextView):void 
		{
			_contextView = contextView;
			_expose && configureExistenceWatcher();
			_inherit && configureExistenceBroadcaster();
		}
		
		private function configureExistenceWatcher():void
		{
			if (_injector.hasDirectMapping(IViewManager))
			{
				_logger.debug("Context拥有IViewManager,使用ViewManager监听context是否存在");
				const viewManager:IViewManager = _injector.getInstance(IViewManager);
				new ViewManagerBasedExistenceWatcher(_context, viewManager);
			}
			else
			{
				_logger.debug("使用context的默认根视图监听context是否存在");
				new ContextViewBasedExistenceWatcher(_context, _contextView.view);
			}
		}
		
		private function configureExistenceBroadcaster():void
		{
			if (_contextView.view.stage)
			{
				broadcastContextExistence();
			}
			else
			{
				_logger.debug("Context view 还没有在stage上，等待中。。。");
				_contextView.displayObjectSignalSet.addedToStage.addOnce(broadcastContextExistence).safelyExecute = true;
			}
		}
		
		private function broadcastContextExistence():void
		{
			_contextView.view.dispatchEvent(new ModularContextEvent(ModularContextEvent.CONTEXT_ADD, _context));
		}
		
		/* INTERFACE xxl.robotlegs.framework.api.IExtension */
		
		public function extend(context:IContext):void 
		{
			context.beforeInitializing(beforeInitializing);
			_context = context;
			_injector = context.injector;
			_logger = context.getLogger(this);
			_context.addConfigHandler(new InstanceOfMatcher(ContextView), handleContextView);
			_injector.map(IModuleConnector).toSingleton(ModuleConnector);
		}
		
	}

}